package com.tianyalei.wolf.mddesignlib.base;


import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import com.tianyalei.wolf.mddesignlib.adater.RecyclerAdapter;
import com.tianyalei.wolf.mddesignlib.base.listener.FreshAndLoadListener;
import com.tianyalei.wolf.mddesignlib.base.page.PageBean;
import com.tianyalei.wolf.mddesignlib.view.autorecyclerview.AutoLoadRecyclerView;
import com.tianyalei.wolf.mddesignlib.view.autorecyclerview.callback.LoadMoreListener;

import java.util.List;

/**
 * Created by wolf on 2016/4/11.
 * 处理上拉刷新和加载更多的工具类
 * <p></p>
 * 用法：在activity里定义recyclerView和refreshLayout，然后实现FreshAndLoadListener接口，
 * 然后设置BaseRecyclerAdapter。在refresh和loadmore方法里取数据，取到后调用该类的freshFinish和loadFinish方法即可
 */
public class RefreshPresenter<T> {

    /**
     * 刷新和加载更多的接口
     */
    private FreshAndLoadListener mFreshAndLoadListener;

    private AutoLoadRecyclerView mAutoLoadRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    /**
     * 分页加载的工具类
     */
    private PageBean mPageBean = new PageBean();

    /**
     * 只有上拉加载更多，没有刷新功能
     *
     * @param recyclerView
     */
    public RefreshPresenter(AutoLoadRecyclerView recyclerView) {
        mAutoLoadRecyclerView = recyclerView;
        recyclerView.setLoadMoreListener(new LoadMoreListener() {
            @Override
            public void loadMore() {
                //由调用者来获取当前页码等数据并处理
                mFreshAndLoadListener.loadMore(mPageBean);
            }
        });
    }

    /**
     * 有下拉刷新和加载更多
     *
     * @param recyclerView
     * @param refreshLayout
     */
    public RefreshPresenter(AutoLoadRecyclerView recyclerView, SwipeRefreshLayout refreshLayout) {
        this(recyclerView);
        mSwipeRefreshLayout = refreshLayout;
        refreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadFirst();
            }
        });
    }

    /**
     * 设置分页用的数据
     *
     * @param pageBean
     */
    public void setPageBean(PageBean pageBean) {
        this.mPageBean = pageBean;
    }

    /**
     * 第一次加载数据
     */
    public void loadFirst() {
        //如果是下拉刷新，那么就对PageBean进行初始化操作
        mPageBean = new PageBean();

        mFreshAndLoadListener.fresh();
    }

    /**
     * 刷新完成调用，刷新失败调用时传null
     *
     * @param list
     */
    public void freshFinish(List<T> list) {
        if (list != null) {
            RecyclerView.Adapter adapter = mAutoLoadRecyclerView.getAdapter();
            if (adapter == null) {
                throw new NullPointerException("请设置适配器");
            }
            if (adapter instanceof RecyclerAdapter) {
                //通知适配器刷新
                ((RecyclerAdapter<T>) adapter).setData(list);
            } else {
                throw new ClassCastException("适配器不合法");
            }

            //页码加1
            mPageBean.setPageNum(mPageBean.getPageNum() + 1);
        }
        if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }


    /**
     * 加载更多完成，失败的话也调用这个方法，传值null
     *
     * @param list
     */
    public void loadFinish(List<T> list) {
        if (list != null) {
            RecyclerView.Adapter adapter = mAutoLoadRecyclerView.getAdapter();
            if (adapter == null) {
                throw new NullPointerException("请设置适配器");
            }
            if (adapter instanceof RecyclerAdapter) {
                //通知适配器刷新
                ((RecyclerAdapter<T>) adapter).addRangeData(list);
            } else {
                throw new ClassCastException("适配器不合法");
            }

            //页码加1
            mPageBean.setPageNum(mPageBean.getPageNum() + 1);
        }
        mAutoLoadRecyclerView.loadFinish(null);
    }

    public void setFreshAndLoadListener(FreshAndLoadListener freshAndLoadListener) {
        this.mFreshAndLoadListener = freshAndLoadListener;
    }
}
